﻿using System;
using System.IO;
using System.Windows.Forms;
using Ro.AssistFunction.ExtentReport;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface;
using Ro.Interpreter.Cores;

namespace Ro.Interpreter.GuiAssist
{
    /// <summary>
    /// 构造类
    /// GUI执行
    /// </summary>
    public class GuiAction
    {
       
        /// <summary>
        /// 执行结果
        /// </summary>
        public bool GuiResult;

        /// <summary>
        /// 构造函数
        /// GUI执行tcs文件
        /// </summary>
        /// <param name="rootNode">送入的treeview的根节点</param>
        public GuiAction(TreeNode rootNode)
        {
            string path = RoPubArgments.GuiTestArgsInfo.TcsFolderPath;
            CheckAllTreeNodes(rootNode,path);  //正式执行
        }


        /// <summary>
        /// 递归方法
        /// 不断递归节点
        /// <para>核心方法</para>
        /// </summary>
        /// <param name="treeNode">Tree下的根节点</param>
        /// <param name="path">给定的母路径</param>
        private void CheckAllTreeNodes(TreeNode treeNode,string path)
        {
            IPreposeFunc preposeFunc= new PreposeFuncs();
            //循环节点
            foreach (TreeNode signode in treeNode.Nodes)
            {
                //如果下属子节点存在
                if (signode.Nodes.Count > 0)
                {
                    path = path + "/" + signode.Text;
                    CheckAllTreeNodes(signode, path); //递归查询
                }
                else
                {
                    //tcs脚本
                    if (signode.Name=="file" && signode.Text.Contains(".tcs")&& signode.Checked)
                    {
                        //完整的tcs路径
                        RoPubArgments.GuiTestArgsInfo.TcsFilePath = path + "/" + signode.Text;
                        //检查和创建临时文件夹
                        string tcsname = Path.GetFileNameWithoutExtension(RoPubArgments.GuiTestArgsInfo.TcsFilePath);
                        CreateTempLog(tcsname);

                        IRoExtentReport roExtentReport = new RoExtentReport(); //初始化ER接口                                               
                        roExtentReport.InitExtentReports(tcsname);//初始化extentreport
                        //执行脚本
                        preposeFunc.CoresFunc(RoPubArgments.GuiTestArgsInfo.TcsFilePath);

                    }
                    //非tcs脚本不处理
                }
                
            }
        }



        /// <summary>
        /// 检查/创建临时Log文件夹、文件
        /// </summary>
        /// <param name="tcsfilename">tcs文件名称</param>
        private void CreateTempLog(string tcsfilename)
        {
            // 创建存放LOG的路径
            string nowtime = DateTime.Now.ToString("yyyy-MM-dd");

            //创建存放log的文件夹
            RoPubArgments.TempLogInfo.FolderPath = "C:/Temp/" + nowtime + "/" + tcsfilename;
            if (!Directory.Exists(RoPubArgments.TempLogInfo.FolderPath))
            {
                Directory.CreateDirectory(RoPubArgments.TempLogInfo.FolderPath);
            }

            //创建存放log的文件夹
            RoPubArgments.TempLogInfo.ImageFolderPath = RoPubArgments.TempLogInfo.FolderPath + "/Image";
            if (!Directory.Exists(RoPubArgments.TempLogInfo.ImageFolderPath))
            {
                Directory.CreateDirectory(RoPubArgments.TempLogInfo.ImageFolderPath);
            }
        }
    }
}
